
function OcrBd() {
    this.to_path = '/sdcard/sceen.png'
    this.access_token = "24.34f08ac0e80a74c1874ab930743d0852.2592000.1724917015.282335-100642768";
}

OcrBd.prototype.init = function () {

}
OcrBd.prototype.ocr = function () {
    captureScreen(this.to_path);
    const start = Date.now();
    const _result = this._getimgtext(this.to_path, this.access_token);
    const end = Date.now();

    console.log(_result);
    console.log('Screenshot detection time: ' + (end - start) + 'ms');
    let result = [];
    for (let i = 0; i < _result.length; i++) {
        let element = _result[i];
        result.push({
            text:element.words,
            bounds:{
                left:element.location.left,
                right:element.location.left + element.location.width,
                top:element.location.top,
                bottom:element.location.top + element.location.height,
            }
        })
    }
    /**
     * 所需格式
     * [
     * text:'',
     * bounds:{left,bottom,top,right}
     * ]
     */
    return result;
}
OcrBd.prototype._getimgtext = function(path, access_token) {
    url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=" + access_token;
    let img = getbase64(path);
    let res = http.post(url, {
        image: img
    }, {
        headers: {
            "Content-Type": "application/x-www-form-urlencoded"
        }
    });
    img = null;
    let html = res.body.json();
    return html.words_result;
}
OcrBd.prototype._getbase64 = function(path) {
    let oin = null;
    oin = new java.io.FileInputStream(path);
    let lt = oin.available();
    let data = java.lang.String((new Array(lt + 1) + "")).getBytes();
    lt = oin.read(data);
    return java.lang.String((android.util.Base64.encode(data, 0)));

}
module.exports = new OcrBd();
